package com.ycsj.goldmedalnewconcept.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;

import android.content.Context;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.TypedValue;

import com.ycsj.goldmedalnewconcept.interf.OnDownListener;




/**
 * 工具类
 * @author Administrator
 *
 */
public class Utils {
	
	/**
	 * 回调接口
	 * 
	 * 1。接口
	 * 
	 * 2。On开头 Listener结尾
	 * 
	 * 3。调用在实现之前。（回调）
	 * 
	 */
	
	
	
	/**
	 * 文件下载
	 * @param url 要下载文件的url
	 * @param dir 要下载到手机目录
	 */
	public static void downLoad(final String url, final File dir, final OnDownListener listener){
		
		new Thread(){
			public void run() {
				
				InputStream in = null;
				OutputStream out = null;
				
				try {
					HttpEntity entity = sendHttpRequest(url);
					in = entity.getContent();
					File file = new File(dir, getFileNameFromUrl(url));
					out = new FileOutputStream(file);
					
					byte[] buffer = new byte[1024];
					int readSize = 0;
					while( (readSize = in.read(buffer) ) != -1){
						out.write(buffer, 0, readSize);
					}
					out.flush();
					  //下载完成
					 //安装
				    // activity.install(file);
					listener.downloadSuccess(file);
				}catch (Exception e) {
					listener.Fail();
					e.printStackTrace();
				}finally{
					try {
						if(in!=null){
							in.close();
						}
						if(out!=null){
							out.close();
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			};
		}.start();
		
	}
	
	
	/**
	 * 获取URL中的文件名
	 * @param url
	 * @return
	 */
	protected static String getFileNameFromUrl(String url) {
		return url.substring(url.lastIndexOf("/")+1);
	}

	/**
	 * 发送Http请求
	 * @param url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static HttpEntity sendHttpRequest(String url) throws ClientProtocolException, IOException{
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet get = new HttpGet(url);
		//设置超时时间
		httpClient.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 3000);
		
		HttpResponse response = httpClient.execute(get);
		
		if(response.getStatusLine().getStatusCode() ==200){
			return response.getEntity();
		}else{
			return null;
		}
	}
	

	/**
	 * 测检网络
	 * @param ctx
	 * @return
	 */
	public static boolean checkNetwork(Context ctx) {
		// step 1 . 连接管理器对象
		ConnectivityManager cm = (ConnectivityManager) ctx
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		// step 2 . 获取当前活动的连接信息对象

		
		// 需要添加android.Manifest.permission.ACCESS_NETWORK_STATE.权限
		NetworkInfo networkInfo = cm.getActiveNetworkInfo();
		boolean rs = false;

		if (networkInfo != null) {
			int type = networkInfo.getType();

			switch (type) {
			case ConnectivityManager.TYPE_WIFI:
			case ConnectivityManager.TYPE_MOBILE:
				rs = true;
				break;
			default:
				break;
			}
		}
		return rs;
	}
	
	  /** 
     *  Dp 2 Pixel 
     */  
    public static int dpToPx(float dp, Resources resources){  
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());  
        return (int) px;  
    } 
    
    
    public static String getCurrentTime(String format) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
		String currentTime = sdf.format(date);
		return currentTime;
	}

	public static String getCurrentTime() {
		return getCurrentTime("yyyy-MM-dd  HH:mm:ss");
	}
}
